rm(list=ls())
setwd("E:/data/cooperation/finance_research/innovation/data")
library(foreign)
library(xlsxjars)
library(xlsx)
library(readstata13)
library(stringr)
library(ggplot2)
library(reshape2)
library(gridExtra)
library(randomForest)
library(e1071)
library(party)
library(mapchina)
library(sysfonts)
library(showtextdb)
library(showtext)
library(tidyverse)
library(rlang)
library(ggplot2)
library(sf)
library(dplyr)
library(RColorBrewer)
library(showtext)
##
da<-read.dta13("co2_finance.dta")


a<- floor((da$year-1999)/10)
a<-as.factor(a)
da<-data.frame(da,year1=a)
q1<-ggplot(da,aes(x=fscale,y=per_co2))+
  geom_point()+
  geom_smooth(method="lm",formula = y ~log(x)+x^3,size=1,color="black")+
  theme_bw()+
  #xlim(1,100)+ 
   ylim(0,20)+
  #scale_color_gradient(low = "gray",high = "black")+
  #scale_fill_manual(name="Year",values=c("gray40", "red"),labels=c('2000-2008','2009-2017'))+
  theme(title=element_text(size=10),text=element_text(size=10,family="serif"))+
  labs(y="Carbon dioxide emissions per capita",x="Financial scale",title="(a) Scatter plot and fitted curve")+
  theme(panel.grid=element_blank(),panel.border=element_blank(),plot.title = element_text(hjust = 0.5),axis.line = element_line())

#
da$dp=round(da$fscale)
dp<-1:8
da_1=data.frame(year=dp)
da_1$mean<-0
for (i in 1:8)
{
  da_s<-da[which(da$dp==(i)),]
  da_1$mean[i]<-mean(da_s$per_co2,na.rm = T)
}



q2<-ggplot(da)+
  geom_boxplot(aes(x=dp,y=per_co2,group=dp),color="black")+
  geom_point(aes(x=dp,y=per_co2),alpha=0.3,position = "jitter",size=0.8)+
  geom_point(data=da_1,aes(x=dp,y=mean),shape=17,size=3)+
  geom_line(data=da_1,aes(x=dp,y=mean),size=1.5,linetype="dotted")+
  theme_bw()+
  ylim(1,20)+
  scale_x_continuous(breaks=seq(1,8,1))+
  labs(x="Financial scale",y=NULL,title="(b) Boxplot and mean curve")+
  theme(title=element_text(size=10),text=element_text(size=10,family="serif"))+
  theme(panel.grid=element_blank(),panel.border=element_blank(),plot.title = element_text(hjust = 0.5),axis.line = element_line())


grid.arrange(q1,q2,ncol=2)

##
df3 <- china 

df3 <- df3 %>%
  group_by(Name_Province) %>%
  summarise(geometry = st_union(geometry))
a<-df3$Name_Province
#write.csv(a,file="中国各省.csv")
b<-read.csv("energy_tech.csv",header=T)
df3$Fossild <- b$energy_d
ggplot(data = df3) +
  geom_sf(aes(fill = Fossild)) +
  scale_fill_distiller(palette = "RdGy",name="Fossil energy dependence (time mean)") +
  theme_bw() +
  labs(title="(b) Area division")+
  theme(text=element_text(size=10,family="serif"))+
  theme(legend.position = "bottom",plot.title = element_text(hjust = 0.5,size=9))



##
df3 <- china 

df3 <- df3 %>%
  group_by(Name_Province) %>%
  summarise(geometry = st_union(geometry))
a<-df3$Name_Province


b<-read.csv("energy_tech.csv",header=T)

df3$Fossild <- b$tech_d
ggplot(data = df3) +
  geom_sf(aes(fill = Fossild)) +
  scale_fill_distiller(palette = "RdGy",name="Technology innovation (time mean)") +
  theme_bw() +
  labs(title="(b) Area division")+
  theme(text=element_text(size=10,family="serif"))+
  theme(legend.position = "bottom",plot.title = element_text(hjust = 0.5,size=9))






